package com.itheima.springbootmybatis.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.itheima.springbootmybatis.domain.Student;
import com.itheima.springbootmybatis.domain.StudentVo;
import com.itheima.springbootmybatis.mapper.StudentMapper;
import com.itheima.springbootmybatis.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService ;

    @Autowired
    private StudentMapper studentMapper ;
    @PostMapping("/aa")
    public Boolean student(@RequestBody Student student){
        String name = student.getName();

        Student student1 = studentMapper.queryName(name);
        if (student1 != null) {
            return  null ;
        }

        boolean b = studentService.updateById(student);
        return b ;
    }

    @PostMapping("/add")
    public Boolean std(@RequestBody Student student){
       // QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
      //  QueryWrapper<Student> wrapper = queryWrapper.eq("name", student.getName());
        int count = studentService.count(student.toQueryWrapperBeforeSave());
        System.out.println(student.toQueryWrapperBeforeSave());
        if (count > 0){
            return false ;
        }
        return  studentService.save(student);
    }


    @PostMapping("/list")
    public Boolean piliang(@RequestParam String ids){
        studentService.removeByIds(Arrays.asList(ids.split(","))) ;
        return true ;
    }

    @PostMapping("/saveList")
    public Map<String ,Object> saveStudent(@RequestBody StudentVo studentVo){
        Map<String ,Object> map = studentService.saveStudent(studentVo.getList());
        return map ;
    }

    /*public static void main(String[] args) {

        List<Student> list = new ArrayList<>() ;
       Student student = new Student("张三");
       Student student1 = new Student("赵六");
       Student student2 = new Student("王五");
       list.add(student);
       list.add(student1);
       list.add(student2);
        System.out.println("list"+list);

        List<String> stringList = new ArrayList<>();

        for (Student student3 : list) {
            stringList.add(student3.getName());
        }
        System.out.println("stringList"+ stringList);


        *//*JSONObject object = new JSONObject();
        object.put("key1",11);
        object.put("key2",22);

        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");

        object.put("key3",list);

        System.out.println(object);

        Map<String,Object> map = new HashMap<>();
        map.put("map1","11");
        map.put("map2","22");
        map.put("list",list);

        System.out.println(map);


         long EXPIRE_TIME = 60 * 60 * 1000;
        System.out.println(EXPIRE_TIME*2/1000);*//*

    }*/
}
